/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 渠道类型下拉框控件
 * 使用说明：
 * jsp中使用范例(最短模式)：<htdz-tag:SelectChannelTypeTag />
 * 			   (最常用模式)：<htdz-tag:SelectChannelTypeTag selected="${xxx}"/>
 * 参数说明：
 * 1. name可以不写,缺省值为"channeltype"
 * 2. id可以不写,缺省值为name的值
 * 3. onchange可以不写，缺省值为无
 * 4. firstoption为首个下拉框的text。可以不写，缺省值为"全部渠道"；
 *    如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则表示此下拉框不需要添加额外option
 * 5. firstvalue为首个下拉框的value。可以不写，缺省值为"MyConstant.SELECT_FIRST_VALUE"   
 * 6. selected为下拉框选中的值，可以不写。
 * 7. includetypes为此下拉框需要包含的渠道类型(多种渠道用英文逗号间隔)。可以不写。如果不写，表示此下拉框包括所有渠道类型。
 * 8. hidedesc：是否隐藏文字说明“渠道类型：”，缺省值为显示
 * 
 * 修订列表： v1.00 2009-12-15 
 * 			修订人：董一华 
 * 			修订内容：初始版本
 * 
 *           v1.01 2010-03-13 
 * 			修订人：董一华 
 * 			修订内容：增加includetypes属性
 * 
 * 			v1.02 2010-06-05 
 * 			修订人：董一华 
 * 			修订内容：新增：hidedesc
 * 
 */

package com.zjht.gmcc.bean.tag;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyParam;

public class SelectChannelTypeTag extends TagSupport {
	private static final long serialVersionUID = 7343412033252888637L;
	private String name;
	private String id;
	private String firstoption;
	private String firstvalue;
	private String onchange;
	private String selected;
	private String includetypes;
	private boolean hidedesc;// 是否隐藏文字说明“渠道类型：”
	private String range;

	public SelectChannelTypeTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {

		String name = this.name;
		String id = this.id;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		String onchange = this.onchange;
		String selected = this.selected;
		String includetypes = this.includetypes;
		boolean hidedesc = this.hidedesc;
		String range = this.range;

		if (name == null || name.length() == 0) {
			name = "channeltype";
		}
		if (id == null || id.length() == 0) {
			id = name;
		}
		boolean hasIncludetypes = (includetypes != null && includetypes.length() > 0);// 是否存在需要包含的渠道类型
		if (hasIncludetypes) {
			if (!includetypes.endsWith(","))
				includetypes += ",";
		}
		if (MyFormat.isStrNull(range)) 
			range = "all";
		range = range.trim().toLowerCase();
		if (!"all".equals(range) && !"fd".equals(range)) 
			throw new JspException("range可以为空；如果不为空，则只能取值：all,fd");
		
		StringBuilder html = new StringBuilder();
		if (!hidedesc) {
			html.append("渠道类型：");
		}
		html.append("<select name=\"");
		html.append(name);
		html.append("\"	id=\"");
		html.append(id);
		html.append("\" ");
		if (onchange != null && onchange.length() > 0) {
			html.append("onchange=\"");
			html.append(onchange);
			html.append("\"");
		}
		html.append(">");
		if (!MyConstant.SELECT_NO_FIRST_OPTION.equals(firstoption))// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
		{
			if (firstoption == null || firstoption.length() == 0)
				firstoption = "全部渠道";

			if (firstvalue == null || firstvalue.length() == 0)
				firstvalue = MyConstant.SELECT_FIRST_VALUE;

			html.append("<option value=\"");
			html.append(firstvalue);
			html.append("\">");
			html.append(firstoption);
			html.append("</option>");
		}
		try {
			if ("all".equals(range) && MyParam.hChannelType != null && MyParam.hChannelType.size() > 0) {
				Iterator<Map.Entry<String, String>> iterator = MyParam.hChannelType.entrySet()
						.iterator();
				String key = null;
				String value = null;
				Map.Entry<String, String> entry = null;
				while (iterator.hasNext()) {
					entry = iterator.next();
					key = entry.getKey();
					value = entry.getValue();
					if (!hasIncludetypes || includetypes.indexOf(key + ",") != -1) {
						html.append("<option value=\"");
						html.append(key);
						html.append("\" ");
						if (selected != null && selected.length() > 0 && key.equals(selected))
							html.append("selected");
						html.append(">");
						html.append(value);
						html.append("</option>");
					}
				}
			}
			else if("fd".equals(range) && MyParam.hChannelType != null && MyParam.hChannelType.size() > 0){
				html.append("<option value=\"");
				html.append(MyConstant.CHANNEL_GMCC_RGS);
				html.append("\" ");
				if (selected != null && selected.length() > 0 && MyConstant.CHANNEL_GMCC_RGS.equals(selected))
					html.append("selected");
				html.append(">");
				html.append("自有渠道+入柜商");
				html.append("</option>");
				
				html.append("<option value=\"");
				html.append(MyConstant.CHANNEL_SOCIAL_PROV);
				html.append("\" ");
				if (selected != null && selected.length() > 0 && MyConstant.CHANNEL_SOCIAL_PROV.equals(selected))
					html.append("selected");
				html.append(">");
				html.append(MyParam.hChannelType.get(MyConstant.CHANNEL_SOCIAL_PROV));
				html.append("</option>");
				
				html.append("<option value=\"");
				html.append(MyConstant.CHANNEL_E_GMCC);
				html.append("\" ");
				if (selected != null && selected.length() > 0 && MyConstant.CHANNEL_E_GMCC.equals(selected))
					html.append("selected");
				html.append(">");
				html.append(MyParam.hChannelType.get(MyConstant.CHANNEL_E_GMCC));
				html.append("</option>");
				
				html.append("<option value=\"");
				html.append(MyConstant.CHANNEL_FD);
				html.append("\" ");
				if (selected != null && selected.length() > 0 && MyConstant.CHANNEL_FD.equals(selected))
					html.append("selected");
				html.append(">");
				html.append(MyParam.hChannelType.get(MyConstant.CHANNEL_FD));
				html.append("</option>");
			}
			html.append("</select>");
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html=null;
		return this.SKIP_BODY;
	}

	public void setName(String newName) {
		name = newName;
	}

	public void setId(String newId) {
		id = newId;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}

	public void setIncludetypes(String includetypes) {
		this.includetypes = includetypes;
	}

	public void setHidedesc(boolean hidedesc) {
		this.hidedesc = hidedesc;
	}

	public void setRange(String range) {
		this.range = range;
	}
}
